#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/**
 * ./testchrdevbase /dev/led 0  关灯
 * ./testchrdevbase /dev/led 1  开灯
 */
int main(int argc, char *argv[])
{
    int fd;
    char buf[1];
    int len;
    int ret;

    /* 1. 判断参数 */
    if (argc < 3)
    {
        printf("Usage: %s /dev/led 0 \n", argv[0]);
        printf("       %s /dev/led 1 \n", argv[0]);
        return -1;
    }
    
    /* 2. 打开文件 */
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        printf("can not open file %s\r\n", argv[1]);
        return -1;
    }
    
    // 开灯或关灯
    if (argc == 3)
    {
        buf[0] = atoi(argv[2]);
        ret = write(fd, buf, 1);
        if(ret < 0)
        {
            printf("write data faild!\r\n");
            return -1;
        }
    }
    
    close(fd);
    
    return 0;
}
